//
//  PPNetError.m
//  PPNetworking
//
//  Created by 邓标祥 on 2020/12/15.
//

#import "PPNetError.h"

@implementation PPNetError

+ (instancetype)errorWithCode:(PPNetErrorCode)code{
    PPNetError *error = [[PPNetError alloc]init];
    error.code = code;
    switch (error.code) {
        case PPNetError_emptyData:
            error.message = @"没有相关数据";
            break;
            
        case PPNetError_formatError:
            error.message = @"服务器数据格式不对";
            break;
            
        case PPNetError_converError:
            error.message = @"转换数据错误";
            break;
            
        default:
            error.message = @"";
            break;
    }
    return error;
}

+ (instancetype)errorWithCode:(NSInteger)code message:(NSString *)message{
    PPNetError *error = [[PPNetError alloc]init];
    error.code = code;
    error.message = message;
    return error;
}

+ (instancetype)errorWithSystemError:(NSError *)systemError{
    if (systemError) {

        NSDictionary *userInfo = systemError.userInfo;
        NSString *errorDescription = @"";

        switch (systemError.code) {
            case NSURLErrorCancelled:
                errorDescription = @"请求已被取消";
                break;
                
            case NSURLErrorBadServerResponse:
                errorDescription = @"糟糕, 网络出错了";
                break;
                
            case NSURLErrorCannotConnectToHost:
                errorDescription = @"手机没有联网，请检查设置";
                break;
                
            case 3840:
                errorDescription = @"服务器数据返回格式有误";
                break;
                
            default:{
                errorDescription = userInfo[NSLocalizedDescriptionKey];
                if (!errorDescription || !errorDescription.length) {
                    errorDescription = @"网络繁忙";
                }
                break;
            };
        }
        PPNetError *ppError = [PPNetError errorWithCode:systemError.code message:errorDescription];
        ppError.systemError = systemError;
        return ppError;
    }
    return nil;
}


- (NSString *)description{
    return [NSString stringWithFormat:@"code=%ld  message = %@", (long)_code, _message];
}

@end
